﻿using DBUtility;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Threading.Tasks;

namespace DBUtility
{
    public class Shopping
    {
        public string AddProduct(string imgurl)
        {
            return "";
        }
        /// <summary>
        /// 获取商品类型
        /// </summary>
        /// <param name="parentid"></param>
        /// <returns></returns>
        public string productset(string parentid)
        {
            string parendids = "";
            DataTable dt = Getparent(parentid);
            if (dt.Rows.Count > 0)
            {
                parendids = dt.Rows[0]["product_name"].ToString();
            }
            else { parendids = ""; }
            return parendids;
        }
        /// <summary>
        /// 添加新产品到数据库
        /// </summary>
        /// <param name="id">产品id</param>
        /// <param name="productparentid">产品父级id</param>
        /// <param name="productname">产品名称</param>
        /// <param name="imageurl">图片的URL</param>
        /// <param name="describe">对产品的描述</param>
        /// <param name="price">产品价格</param>
        /// <param name="model">产品的类型</param>
        /// <param name="stock">库存</param>
        /// <param name="productset">产品设计属于热销产品还是什么类型的</param>
        /// <returns></returns>
        public  string addproducts(string id, string productparentid,string productname,string productset) {
            Sqlopenhander doh = new Sqlopenhander();
            doh.Reset();
            doh.add_alFieldItems("product_id", id);
            doh.add_alFieldItems("parent_id", productparentid);
            doh.add_alFieldItems("product_name", productname);
            doh.add_alFieldItems("productset", productset);
            doh.add_alFieldItems("deletes", "1");
            doh.add_alFieldItems("Go_online", "0");
            doh.add_alFieldItems("Go_onlinetime",DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"));
            doh.add_alFieldItems("createtime", DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"));
            doh.Insert("Product");
            doh.Dispose();
            return "";
        }
        /// <summary>
        /// 把商品的属性插入属性表
        /// </summary>
        /// <param name="product_id">产品的id</param>
        /// <param name="product_type">产品型号</param>
        /// <param name="product_detale">产品的描述</param>
        /// <param name="main_img">主商品的图片</param>
        /// <param name="img">这个型号的图片</param>
        /// <param name="Stock">库存</param>
        /// <param name="price">价格</param>
        public  void insertProduct_setAttribute(string product_id, string product_type, string product_detale, string main_img, string childimg, int Stock, string price)
        {
            Sqlopenhander doh = new Sqlopenhander();
            doh.Reset();
            doh.add_alFieldItems("id",Guid.NewGuid().ToString());
            doh.add_alFieldItems("product_id", product_id);
            doh.add_alFieldItems("product_type", product_type);
            doh.add_alFieldItems("product_detale", product_detale);
            doh.add_alFieldItems("main_img", main_img);
            doh.add_alFieldItems("childimg", childimg);
            doh.add_alFieldItems("Stock", Stock);
            doh.add_alFieldItems("price", price);
            doh.Insert("Product_attributes");
            doh.Dispose();
        }
        /// <summary>
        /// 根据parentid获取表数据
        /// </summary>
        /// <param name="parentid"></param>
        /// <returns></returns>
        public static DataTable Getparent(string parentid)
        {
           
            Sqlopenhander doh = new Sqlopenhander();
            doh.Reset();
            doh.Sqlcmd = " select * from Product where product_id=@id and deletes='1'";
            doh.add_alConditionParameters("@id", parentid);
            DataTable DT = doh.GetDataTable();
            doh.Dispose();
            return DT;
        }
        public  DataTable Getindexdata() {
            Sqlopenhander doh = new Sqlopenhander();
            doh.Reset();
            doh.Sqlcmd = "SELECT * FROM View_product WHERE id IN ( select min(id) from View_product group by product_id ) "+
            " and deletes='1' and Go_online='0' order by  createtime desc";
            DataTable DT = doh.GetDataTable();
            doh.Dispose();
            return DT;
        
        }
        public void submitOrder(string data, string guid, string customerdata)
        {
            string[] customerdataareey = customerdata.Split('|');
            string[] sushe;
            string building = "";
            string dorm = "";
            try {
                 sushe = customerdataareey[2].Split('栋');
                 building = sushe[0];
                 dorm = sushe[1];
              
            }
          catch(Exception ex){
              building = customerdataareey[2];
              dorm = customerdataareey[2];
          }
            string[] areey = data.Split('|');
            for (int i = 3; i < areey.Length; i++)
            {
                string[] productdata = areey[i].Split('@');
                Sqlopenhander doh = new Sqlopenhander();
                doh.Reset();
                doh.add_alFieldItems("id", Guid.NewGuid().ToString());
                doh.add_alFieldItems("openid", areey[0]);
                doh.add_alFieldItems("pruduct_id", productdata[0]);
                doh.add_alFieldItems("payway", areey[2]);
                doh.add_alFieldItems("leave_word", areey[1]);
                doh.add_alFieldItems("sended", "1");
                doh.add_alFieldItems("building", building);
                doh.add_alFieldItems("dorm", dorm);
                doh.add_alFieldItems("phone", customerdataareey[1]);
                doh.add_alFieldItems("amount", productdata[1]);
                doh.add_alFieldItems("payed", "1");
                doh.add_alFieldItems("finished", "1");
                doh.add_alFieldItems("deletes", "1");
                doh.add_alFieldItems("createtime", DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"));
                doh.Insert("Orders_Record");
                doh.Dispose();
            }
        }

        public void insertcustom(string data,string guid)
        {
            string[] areey = data.Split('|');
            Sqlopenhander doh = new Sqlopenhander();
            doh.Reset();
            doh.ConditionExpress = "id=@name";
            doh.add_alConditionParameters("@name", guid);
            doh.add_alFieldItems("customer_phone", areey[1]);
            doh.add_alFieldItems("costomer_address", areey[2]);
            int dd = doh.Update("Orders_Record");
            doh.Dispose();
        }

        public void addshowpinghcar(string data)
        {
            string[] areey = data.Split('|');
            for (int i = 1; i < areey.Length; i++)
            {
                Sqlopenhander doh = new Sqlopenhander();
                doh.Reset();
                doh.add_alFieldItems("id", Guid.NewGuid().ToString());
                doh.add_alFieldItems("openid", areey[0]);
                doh.add_alFieldItems("pruduct_id", areey[i]);
                doh.add_alFieldItems("deletes", "1");
                doh.add_alFieldItems("createtime", DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"));
                doh.Insert("ShoppingCar");
                doh.Dispose();
            }
        }
        public void handleordermanager(string data) {
            string[] areey = data.Split('|');
            Sqlopenhander doh = new Sqlopenhander();
            doh.Reset();
            doh.ConditionExpress = "id=@name";
            doh.add_alConditionParameters("@name", areey[1]);
            if (areey[0] == "0") {
                doh.add_alFieldItems("deletes", "2");
            }
            else if (areey[0] == "1") {
                doh.add_alFieldItems("sended", "2");
            }
            else if (areey[0] == "2") {
                doh.add_alFieldItems("sended", "0");
            }
            int dd = doh.Update("Orders_Record");
            doh.Dispose();
        
        }

        public void deleteshoppingcar(string data)
        {
            string[] areey = data.Split('|');
            for (int i = 1; i<areey.Length;i++ )
            {
                Sqlopenhander doh = new Sqlopenhander();
                doh.Reset();
                doh.ConditionExpress = "openid=@name and id=@id";
                doh.add_alConditionParameters("@name", areey[0]);
                doh.add_alConditionParameters("@id", areey[i]);
                doh.add_alFieldItems("deletes", "0");
                int dd = doh.Update("ShoppingCar");
                doh.Dispose();
            }
        }
        public  DataTable goodmanager(int page_current, string where, ref int record_count)
        {
            string where1 = " 1=1 and deletes='1' ";
            string[] areey = where.Split('|');
            if (areey[0] != "") {
                where1 += " and Go_online=" + areey[0]; 
            }
            else if (areey[1] != "") {
                where1 += " and ( product_name like '%" + areey[1] + "%' or productset like '%" + areey[1] + "%' or product_type  like '%" + areey[1] + "%')";
            }
            string table_name = "View_product";
            string fieldkey = "";
            string field_show = "id";
            string field_order = " createtime DESC ";
            int page_size = 10;
            DataTable database = DBUtility.Sqlopenhander.QueryPagedDt(table_name, fieldkey, page_current, page_size, field_show, field_order, where1, ref record_count);
            return database;
        }

        public DataTable classifiedmanager(int page_current, string where, ref int record_count)
        {
            string where1 = " 1=1 and deletes='1' and parent_id='0'";
            string[] areey = where.Split('|');
            string table_name = "Product";
            string fieldkey = "";
            string field_show = "product_id";
            string field_order = " createtime DESC ";
            int page_size = 10;
            DataTable database = DBUtility.Sqlopenhander.QueryPagedDt(table_name, fieldkey, page_current, page_size, field_show, field_order, where1, ref record_count);
            return database;
        }
        public DataTable manmanager(int page_current, string where, ref int record_count)
        {
            string where1 = " 1=1 and state='0' and user_type='1'";
            string[] areey = where.Split('|');
            string table_name = "manager";
            string fieldkey = "";
            string field_show = "id";
            string field_order = " createtime DESC ";
            int page_size = 10;
            DataTable database = DBUtility.Sqlopenhander.QueryPagedDt(table_name, fieldkey, page_current, page_size, field_show, field_order, where1, ref record_count);
            return database;
        }

        public DataTable aftersale(int page_current, string where, ref int record_count)
        {
            string where1 = " 1=1 and  deletes='1'";
            string[] arrey = where.Split('|');
            if (arrey[0] !="")
            {
                string dd = arrey[0] == "4" ? "'4'" : "'2','3'";
                where1 += " and finished in(" + dd + ")  ";
            }
            else {
                where1 += " and finished in('3','2','4')  ";
            }
            if (arrey[1] != "") {
                where1 += " and ( product_name like '%" + arrey[1] + "%')";
            }
            string table_name = "View_Order_record";
            string fieldkey = "";
            string field_show = "id";
            string field_order = " createtime DESC ";
            int page_size = 10;
            DataTable database = DBUtility.Sqlopenhander.QueryPagedDt(table_name, fieldkey, page_current, page_size, field_show, field_order, where1, ref record_count);
            return database;
        }



        public DataTable GetAboutdata(string productid)
        {
            Sqlopenhander doh = new Sqlopenhander();
            doh.Reset();
            doh.Sqlcmd = " select * from View_product where product_id=@id and deletes='1'";
            doh.add_alConditionParameters("@id", productid);
            DataTable DT = doh.GetDataTable();
            doh.Dispose();
            return DT;

        }
        public DataTable getaddproduct()
        {
            Sqlopenhander doh = new Sqlopenhander();
            doh.Reset();
            doh.Sqlcmd = " select * from Product where parent_id='0' and deletes='1'";
            DataTable DT = doh.GetDataTable();
            doh.Dispose();
            return DT;

        }
        /// <summary>
        /// 根据属性id去获取产品
        /// </summary>
        /// <param name="productdata"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public DataTable Getbuydata(string productdata,string type)
        {
            string[] areey = productdata.Split('|');
            DataTable DT;
            if (type == "Product") {
                string limite = "";
                for (int i = 0; i < areey.Length; i++)
                {
                    if (i == 0)
                    {
                        limite += "'" + areey[i] + "'";
                    }
                    else { limite += ",'" + areey[i] + "'"; }
                }
                Sqlopenhander doh = new Sqlopenhander();
                doh.Reset();
                doh.Sqlcmd = " select * from View_product where id in(" + limite + ") and deletes='1'";
                DT = doh.GetDataTable();
                doh.Dispose();
            }
            else {
                string limite = "";
                int li=areey.Length-1;
                for (int i = 0; i < li; i++)
                {
                    if (i == 0)
                    {
                        limite += "'" + areey[i] + "'";
                    }
                    else { limite += ",'" + areey[i] + "'"; }
                }
                Sqlopenhander doh = new Sqlopenhander();
                doh.Reset();
                doh.Sqlcmd = " select product_name,main_img,Stock,Sales_volume,price,openid,childimg,pruduct_id as id"+
                    " from ShopingCar_Pro where id in(" + limite + ") and openid=@openid and deletes='1'";
                doh.add_alConditionParameters("@openid", areey[li]);
                 DT = doh.GetDataTable();
                 doh.Dispose();
            }
           
            return DT;

        }
        public DataTable Getshowpingcar(string openid)
        {
            Sqlopenhander doh = new Sqlopenhander();
            doh.Reset();
            doh.Sqlcmd = " select * from ShopingCar_Pro where openid=@id and deletes='1' AND  ShoppingCar_deletes='1'";
            doh.add_alConditionParameters("@id", openid);
            DataTable DT = doh.GetDataTable();
            doh.Dispose();
            return DT;

        }

        public DataTable Getorderpage(string openid)
        {
            Sqlopenhander doh = new Sqlopenhander();
            doh.Reset();
            doh.Sqlcmd = " select * from View_Order_record where openid=@id and deletes='1' order by finished desc ";
            doh.add_alConditionParameters("@id", openid);
            DataTable DT = doh.GetDataTable();
            return DT;

        }
        public DataTable Getordermanager(int page_current, string where, ref int record_count)
        {
            string where1 = " 1=1 and  deletes !='2'";
            string[] areey = where.Split('|');
            where1 += areey[0] != "" ? " and sended='"+areey[0]+"'" : "";
            where1 += areey[1] != "" ? " and createtime between '" + areey[1] + "' and '" + areey[2] + "'" : "";
            where1 += areey[3] != "" ? " and product_name like '%" + areey[3] + "%'" : "";
            string table_name = "View_Order_record";
            string fieldkey = "";
            string field_show = "id";
            string field_order = " createtime DESC ";
            int page_size = 10;
            DataTable database = DBUtility.Sqlopenhander.QueryPagedDt(table_name, fieldkey, page_current, page_size, field_show, field_order, where1, ref record_count);
            return database;

        }

        public DataTable Getuserdata(string openid)
        {
            Sqlopenhander doh = new Sqlopenhander();
            doh.Reset();
            doh.Sqlcmd = " select * from clientlist where openid=@id and state='0'";
            doh.add_alConditionParameters("@id", openid);
            DataTable DT = doh.GetDataTable();
            doh.Dispose();
            return DT;

        }
    }
}
